#define GUILITE_ON  //Do not define this macro once more!!!
#include "GuiLite.h"
#include <stdlib.h>
#include <string.h>
#include "font.h"


#define EMITTER_X 120
#define EMITTER_Y 250
#define ACC_X 0
#define ACC_Y 1
#define PARTICAL_WITH	3
#define PARTICAL_HEIGHT 3

c_display* s_display;
static c_surface* s_surface;


/*warning:could display X*/
static void draw_string_lcd(const char* s,unsigned short len)
	{
		char ch;
		static unsigned int lcd_x = 0,lcd_y = 0;
		int i = 0;
		
		if (0 == s){
			return;
		}
		 for(i = 0;i < len;i++)
    {
        ch = s[i];
        lcd_x += 16;
			
        if(((ch == '\n') || (ch == '\r'))){
						lcd_x = 0;
            lcd_y += 16;
        } 
        if(lcd_y > UI_HEIGHT)
    {
			s_surface->fill_rect(0, 0, UI_WIDTH - 1, UI_HEIGHT - 1, 0, Z_ORDER_LEVEL_0);		//warning:this part can't clear scarn
      lcd_x = 0,lcd_y = 0;
    } 
		c_word::draw_char(s_surface, Z_ORDER_LEVEL_0, ch, lcd_x, lcd_y, c_theme::get_font(FONT_CUSTOM1), GL_RGB(255, 255, 255), GL_ARGB(0, 0, 0, 0));
    }

	}

//////////////////////// start UI ////////////////////////	�����ֿ�
extern const LATTICE_FONT_INFO Microsoft_YaHei_28;
extern const LATTICE_FONT_INFO STM_string;
void load_resource() {
	c_theme::add_font(FONT_DEFAULT, &Microsoft_YaHei_28);	//test store
	c_theme::add_font(FONT_CUSTOM1,&STM_string);	//string store
}


void create_ui(void* phy_fb, int screen_width, int screen_height, int color_bytes, struct EXTERNAL_GFX_OP* gfx_op) {
	load_resource();
	if (phy_fb)
	{
		static c_surface surface(UI_WIDTH, UI_HEIGHT, color_bytes, Z_ORDER_LEVEL_0);
		static c_display display(phy_fb, screen_width, screen_height, &surface);
		s_surface = &surface;
		s_display = &display;
	}
	else
	{//for MCU without framebuffer
		static c_surface_no_fb surface_no_fb(UI_WIDTH, UI_HEIGHT, color_bytes, gfx_op, Z_ORDER_LEVEL_0);
		static c_display display(phy_fb, screen_width, screen_height, &surface_no_fb);
		s_surface = &surface_no_fb;
		s_display = &display;
	}

	s_surface->fill_rect(0, 0, UI_WIDTH - 1, UI_HEIGHT - 1, 0, Z_ORDER_LEVEL_0);	//��仭��
	
}

//////////////////////// interface for all platform ////////////////////////
extern "C" void startHelloParticle(void* phy_fb, int width, int height, int color_bytes, struct EXTERNAL_GFX_OP* gfx_op) {
	create_ui(phy_fb, width, height, color_bytes, gfx_op);
}

extern "C" void* getUiOfHelloParticle(int* width, int* height, bool force_update = false)
{
	if (s_display)
	{
		return s_display->get_updated_fb(width, height, force_update);
	}
	return NULL;
}

extern "C" void StarGuiLcd(const char* string,unsigned short len){
	draw_string_lcd(string,len);
}
